package com.jianyun.jyzs.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;


import com.jianyun.jyzs.R;
import com.jianyun.jyzs.activity.CrmCalendarActivity;
import com.jianyun.jyzs.bean.CrmCalendarCountBean;
import com.jianyun.jyzs.bean.CrmDetial;
import com.jianyun.jyzs.bean.DateInfo;
import com.jianyun.jyzs.utils.DateUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by mlli on 2017/8/31.
 */

public class CalendarAdapter extends RecyclerView.Adapter {
    private CrmCalendarActivity context;
    private List<DateInfo> list;
    private List<CrmCalendarCountBean.DataList> dataCunt;
    private String dateFomat = "yyyy-MM-dd";
    private String toDay = "";
    private int tabIndexs;
    private String dateSelect;


    @SuppressLint("WrongConstant")
    public CalendarAdapter(CrmCalendarActivity context, List<DateInfo> list) {
        context.getSystemService(Context.WINDOW_SERVICE);
        this.context = context;
        this.list = list;
        toDay = DateUtils.getStringToday(dateFomat);
    }

    public void setList(List<DateInfo> list, List<CrmCalendarCountBean.DataList> dataCunt, int tabIndex,String dateSelect) {

        this.list = list;
        this.dataCunt = dataCunt;
        tabIndexs = tabIndex;
        this.dateSelect=dateSelect;
        Log.i("test","设置的日期："+dateSelect);
        for (int i = 0; i < list.size(); i++) {
            DateInfo dateInfo = list.get(i);
            Date date = dateInfo.getDate();
            String dateToString = DateUtils.dateToString(date, dateFomat);
            if (dateToString.equals(dateSelect)) {
                Log.i("test","选择的日期："+dateToString);
                dateInfo.setSelected(true);
                context.setCalendarViewDateClick(dateToString);
            } else {
                dateInfo.setSelected(false);
            }


        }

        notifyDataSetChanged();

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.wo_calendar_item, null);
        return new CalendarAdapter.MyHolder(view);
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final CalendarAdapter.MyHolder myHolder = (CalendarAdapter.MyHolder) holder;
        int screenWidth = getScreenWidth(context);
        myHolder.itemView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, screenWidth / 8));
        final DateInfo info = list.get(position);
        final Date date = info.getDate();
        final String dateToString = DateUtils.dateToString(date, dateFomat);
        myHolder.count.setVisibility(View.INVISIBLE);
        if (null != dataCunt && dataCunt.size() > 0) {
            int size = dataCunt.size();
            for (int i = 0; i < size; i++) {
                String jldate = dataCunt.get(i).getJldate();
                int count1 = dataCunt.get(i).getCount1();
                if (dateToString.equals(jldate)) {
                    Log.i("test","日期："+dateToString);
                    myHolder.count.setText(count1 + "");
                    boolean compareDate = DateUtils.compare_date(jldate, toDay);
                    if (compareDate && 1 == tabIndexs) {
                        myHolder.count.setBackground(context.getResources().getDrawable(R.drawable.right_top_grey));
                        myHolder.count.setVisibility(View.VISIBLE);
                    } else {
                        Log.i("test","蓝色日期："+info.getDate().toString());
                        myHolder.count.setBackground(context.getResources().getDrawable(R.drawable.right_top));
                        myHolder.count.setVisibility(View.VISIBLE);
                    }

                }else {
                    //myHolder.count.setVisibility(View.INVISIBLE);
                }
            }
        }

        myHolder.tv.setText(info.getDayOfM() + "");


        int type = info.getType();
        switch (type) {
            case 1:
                myHolder.tv.setTextColor(Color.GRAY);
                myHolder.itemView.setEnabled(false);
                break;
            case 2:
                myHolder.tv.setTextColor(Color.BLACK);
                myHolder.itemView.setEnabled(true);
                break;
            case 3:
                myHolder.tv.setTextColor(Color.GRAY);
                myHolder.itemView.setEnabled(false);
                break;
        }
        if (info.isToday()) {
            myHolder.tv.setTextColor(context.getResources().getColor(R.color.blue));
        }else {

        }
        if (info.isSelected()) {
            Log.i("test","选择日期："+info.getDate().toString());
            myHolder.tv.setBackgroundResource(R.drawable.round_blue);
            myHolder.tv.setTextColor(context.getResources().getColor(R.color.white));

        }else {
            myHolder.tv.setBackgroundColor(context.getResources().getColor(R.color.white));
        }


        myHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < list.size(); i++) {
                    DateInfo dateInfo = list.get(i);
                    dateInfo.setSelected(false);
                }

                info.setSelected(true);
                notifyDataSetChanged();
                context.setCalendarViewDateClick(dateToString);


            }
        });
    }


    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {

        private final TextView tv;
        private final TextView count;

        public MyHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.calendar_tv);
            count = (TextView) itemView.findViewById(R.id.count);
        }
    }

    @SuppressLint("WrongConstant")
    private int getScreenWidth(Context context) {
        return ((WindowManager) (context.getSystemService(Context.WINDOW_SERVICE))).getDefaultDisplay().getWidth();
    }
}